
from flask import Flask, render_template
from flask_session import Session


from config import CONFIG
from flask_sqlalchemy import SQLAlchemy
# from app.views.index import index_blue

from app2.utils.common.common import show_index_colorful


db = SQLAlchemy()  # 创建SQLAlchemy对象，后面在create_app函数与app对象进行关联


def create_app(config_name):
    from app2.views.index import index_blue
    from app2.views.passport import passport_blue
    from app2.views.user import user_blue
    from app2.views.news import news_blue
    from app2.views.admin import admin_blue
    from app2.views.games import games_blue

    # 创建flask对象
    app = Flask(__name__)
    # 对flask对象进行配置
    app.config.from_object(CONFIG.get(config_name))
    # 注册蓝图
    app.register_blueprint(index_blue)
    app.register_blueprint(passport_blue)
    app.register_blueprint(user_blue, url_prefix="/user")
    app.register_blueprint(news_blue, url_prefix="/news")
    app.register_blueprint(admin_blue, url_prefix="/admin")
    app.register_blueprint(games_blue, url_prefix="/games")
    # db 与 app 进行关联
    db.init_app(app)
    # 创建Session对象
    Session(app)
    # 获取路由表
    # print('='*80)
    # print(app.url_map)
    # print('=' * 80)
    # 注册过滤器
    app.add_template_filter(show_index_colorful, "show_index_colorful")

    @app.errorhandler(404)
    def page_404(e):
        return render_template('index/404.html'), 404
    return app


